﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autofac;
using ComputerVision.Data;
using ComputerVision.Services;

namespace ComputerVision.Common
{
    public class FileUtil
    {
        private const string AnhRaFolder = "AnhRa";
        private const string AnhVaoFolder = "AnhVao";
        private const string BienSoVaoFolder = "BienSoVao";
        private const string BienSoRaFolder = "BienSoRa";
        private const string DataFolder = "NhatMinhSSCData";
        private string dirAnhRa = string.Empty;
        private string dirAnhVao = string.Empty;
        private string dirBienSoRa = string.Empty;
        private string dirBienSoVao = string.Empty;

        private string dirAnhRaUrl = string.Empty;
        private string dirAnhVaoUrl = string.Empty;
        private string dirBienSoRaUrl = string.Empty;
        private string dirBienSoVaoUrl = string.Empty;

        //private string dataServerIp = string.Empty;
        private IService<ConfigrationCamera> _configCamera;
        private string _computerName = string.Empty;
        public string tempFolderToday = string.Empty;
        //private string tempFolder = string.Empty;
        private bool flag = false;
        public FileUtil()
        {
            _computerName = Environment.MachineName;
            _configCamera = Program.Container.Resolve<IService<ConfigrationCamera>>();

            if (!ValidDirectory())
            {
                MessageBox.Show(@"Không thể tạo thư mục lưu dữ liệu. Vui lòng xem lại thiết lập.");
            }

        }

        private bool ValidDirectory()
        {
            var item = _configCamera.ToList().ToList().FirstOrDefault(p => p.ComputerName.Contains(_computerName));
            tempFolderToday = DateTime.Today.Year.ToString("d4") + DateTime.Today.Month.ToString("d2") + DateTime.Today.Day.ToString("d2");

            if (item != null)
            {
                if (item.OnLAN == true)
                {
                    try
                    {
                        if (!Directory.Exists(@"\\" + item.ServerData + @"\" + DataFolder))
                        {
                            Directory.CreateDirectory(@"\\" + item.ServerData + @"\" + DataFolder);
                        }

                        //AnhRaFolder
                        if (!Directory.Exists(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhRaFolder))
                        {
                            Directory.CreateDirectory(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhRaFolder);
                        }

                        dirAnhRa = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhRaFolder + @"\" + tempFolderToday;
                        dirAnhRaUrl = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhRaFolder + @"\";
                        if (!Directory.Exists(dirAnhRa))
                        {
                            Directory.CreateDirectory(dirAnhRa);
                        }

                        //AnhVaoFolder
                        if (!Directory.Exists(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhVaoFolder))
                        {
                            Directory.CreateDirectory(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhVaoFolder);

                        }

                        dirAnhVao = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhVaoFolder + @"\" + tempFolderToday;
                        dirAnhVaoUrl = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + AnhVaoFolder + @"\";
                        if (!Directory.Exists(dirAnhVao))
                        {
                            Directory.CreateDirectory(dirAnhVao);
                        }

                        //BienSoVaoFolder
                        if (!Directory.Exists(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoVaoFolder))
                        {
                            Directory.CreateDirectory(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoVaoFolder);
                        }

                        dirBienSoVao = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoVaoFolder + @"\" + tempFolderToday;
                        dirBienSoVaoUrl = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoVaoFolder + @"\";
                        if (!Directory.Exists(dirBienSoVao))
                        {
                            Directory.CreateDirectory(dirBienSoVao);
                        }

                        //BienSoRaFolder
                        if (!Directory.Exists(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoRaFolder))
                        {
                            Directory.CreateDirectory(@"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoRaFolder);
                        }

                        dirBienSoRa = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoRaFolder + @"\" + tempFolderToday;
                        dirBienSoRaUrl = @"\\" + item.ServerData + @"\" + DataFolder + @"\" + BienSoRaFolder + @"\";
                        if (!Directory.Exists(dirBienSoRa))
                        {
                            Directory.CreateDirectory(dirBienSoRa);
                        }

                        flag = true;
                    }
                    catch (Exception)
                    {
                        flag = false;

                    }
                }
                else
                {
                    try
                    {
                        if (!Directory.Exists(item.ServerData + DataFolder))
                        {
                            Directory.CreateDirectory(item.ServerData + DataFolder);
                        }

                        //AnhRaFolder
                        if (!Directory.Exists(item.ServerData + DataFolder + @"\" + AnhRaFolder))
                        {
                            Directory.CreateDirectory(item.ServerData + DataFolder + @"\" + AnhRaFolder);
                        }

                        dirAnhRa = item.ServerData + DataFolder + @"\" + AnhRaFolder + @"\" + tempFolderToday;
                        dirAnhRaUrl = item.ServerData + DataFolder + @"\" + AnhRaFolder + @"\";
                        if (!Directory.Exists(dirAnhRa))
                        {
                            Directory.CreateDirectory(dirAnhRa);
                        }

                        //AnhVaoFolder
                        if (!Directory.Exists(item.ServerData + DataFolder + @"\" + AnhVaoFolder))
                        {
                            Directory.CreateDirectory(item.ServerData + DataFolder + @"\" + AnhVaoFolder);

                        }

                        dirAnhVao = item.ServerData + DataFolder + @"\" + AnhVaoFolder + @"\" + tempFolderToday;
                        dirAnhVaoUrl = item.ServerData + DataFolder + @"\" + AnhVaoFolder + @"\";
                        if (!Directory.Exists(dirAnhVao))
                        {
                            Directory.CreateDirectory(dirAnhVao);
                        }

                        //BienSoVaoFolder
                        if (!Directory.Exists(item.ServerData + DataFolder + @"\" + BienSoVaoFolder))
                        {
                            Directory.CreateDirectory(item.ServerData + DataFolder + @"\" + BienSoVaoFolder);
                        }

                        dirBienSoVao = item.ServerData + DataFolder + @"\" + BienSoVaoFolder + @"\" + tempFolderToday;
                        dirBienSoVaoUrl = item.ServerData + DataFolder + @"\" + BienSoVaoFolder + @"\";
                        if (!Directory.Exists(dirBienSoVao))
                        {
                            Directory.CreateDirectory(dirBienSoVao);
                        }

                        //BienSoRaFolder
                        if (!Directory.Exists(item.ServerData + DataFolder + @"\" + BienSoRaFolder))
                        {
                            Directory.CreateDirectory(item.ServerData + DataFolder + @"\" + BienSoRaFolder);
                        }

                        dirBienSoRa = item.ServerData + DataFolder + @"\" + BienSoRaFolder + @"\" + tempFolderToday;
                        dirBienSoRaUrl = item.ServerData + DataFolder + @"\" + BienSoRaFolder + @"\";
                        if (!Directory.Exists(dirBienSoRa))
                        {
                            Directory.CreateDirectory(dirBienSoRa);
                        }

                        flag = true;
                    }
                    catch (Exception)
                    {
                        flag = false;

                    }
                }


            }
            else
            {
                flag = false;
            }
            return flag;
        }

        public bool WriteImagesVao(ImgageArrayVao imgageArray)
        {
            bool result = false;
            try
            {
                //if (imgageArray.ImageTruoc != null)
                imgageArray.ImageTruoc.Save(dirAnhVao + @"\" + imgageArray.NameTruoc, System.Drawing.Imaging.ImageFormat.Jpeg);

                //if (imgageArray.ImageSau != null)
                imgageArray.ImageSau.Save(dirAnhVao + @"\" + imgageArray.NameSau, System.Drawing.Imaging.ImageFormat.Jpeg);

                if (imgageArray.ImageBienSo != null)
                    imgageArray.ImageBienSo.Save(dirBienSoVao + @"\" + imgageArray.NameBienSo, System.Drawing.Imaging.ImageFormat.Jpeg);

                result = true;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
                result = false;
            }

            return result;
        }

        public string GenerateLink(string type, string name)
        {
            string result = string.Empty;

            switch (type.ToUpper())
            {

                case "ANHRA":
                    result = dirAnhRaUrl + @"\" + name;
                    break;

                case "ANHVAO":
                    result = dirAnhVaoUrl + @"\" + name;
                    break;

                case "BIENSORA":
                    result = dirBienSoRaUrl + @"\" + name;
                    break;

                case "BIENSOVAO":
                    result = dirBienSoVaoUrl + @"\" + name;
                    break;
            }
            return result;
        }

        public bool WriteImagesRa(ImgageArrayRa imgageArray)
        {
            bool result = false;
            try
            {
                if (imgageArray.ImageTruoc != null)
                    imgageArray.ImageTruoc.Save(dirAnhRa + @"\" + imgageArray.NameTruoc, System.Drawing.Imaging.ImageFormat.Jpeg);

                if (imgageArray.ImageSau != null)
                    imgageArray.ImageSau.Save(dirAnhRa + @"\" + imgageArray.NameSau, System.Drawing.Imaging.ImageFormat.Jpeg);

                if (imgageArray.ImageBienSo != null)
                    imgageArray.ImageBienSo.Save(dirBienSoRa + @"\" + imgageArray.NameBienSo, System.Drawing.Imaging.ImageFormat.Jpeg);
                result = true;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
                result = false;

            }
            return result;
        }

    }

    public class ImgageArrayVao
    {
        public Image ImageTruoc;
        public Image ImageSau;
        public Image ImageBienSo;
        public string NameTruoc;
        public string NameSau;
        public string NameBienSo;
        public string NameTruocUrl;
        public string NameSauUrl;
        public string NameBienSoUrl;
        private string tempFolderToday = string.Empty;

        public ImgageArrayVao(Image truoc, Image sau, Image bienSo, string name, string folder)
        {
            tempFolderToday = folder;
            this.ImageTruoc = truoc;
            this.ImageSau = sau;
            this.ImageBienSo = bienSo;
            this.NameTruocUrl = tempFolderToday + @"\" + name + "V1.jpg";
            this.NameSauUrl = tempFolderToday + @"\" + name + "V2.jpg";
            this.NameBienSoUrl = tempFolderToday + @"\" + name + "BV.jpg";
            this.NameTruoc = name + "V1.jpg";
            this.NameSau = name + "V2.jpg";
            this.NameBienSo = name + "BV.jpg";
        }
    }

    public class ImgageArrayRa
    {
        public Image ImageTruoc;
        public Image ImageSau;
        public Image ImageBienSo;
        public string NameTruoc;
        public string NameSau;
        public string NameBienSo;
        public string NameTruocUrl;
        public string NameSauUrl;
        public string NameBienSoUrl;
        private string tempFolderToday = string.Empty;

        public ImgageArrayRa(Image truoc, Image sau, Image bienSo, string name, string folder)
        {
            tempFolderToday = folder;
            this.ImageTruoc = truoc;
            this.ImageSau = sau;
            this.ImageBienSo = bienSo;
            this.NameTruocUrl = tempFolderToday + @"\" + name + "V1.jpg";
            this.NameSauUrl = tempFolderToday + @"\" + name + "V2.jpg";
            this.NameBienSoUrl = tempFolderToday + @"\" + name + "BR.jpg";
            this.NameTruoc = name + "V1.jpg";
            this.NameSau = name + "V2.jpg";
            this.NameBienSo = name + "BR.jpg";
        }
    }
}
